using System;
using System.Collections.Generic;
using System.Text;

namespace SP
{
    public class Utils
    {

        //public static Preprocessor GetPreprocessor(IFusion f, string ppName)
        //{
        //    foreach (Preprocessor p in f.Preprocessors)
        //        if (p.Name.Equals(ppName))
        //            return p;

        //    return null;
        //}

        //public static IParameter GetParameter(IFusion f, string paName)
        //{
        //    foreach (IParameter p in f.Parameters)
        //        if (p.Name.Equals(paName))
        //            return p;

        //    return null;
        //}

        public static List<IParameter> GetParametersByType(IFusion fusion, Type t, bool editableOnly)
        {
            if (null == fusion)
                return null;

            List<IParameter> ret = new List<IParameter>();

            foreach (IParameter p in fusion.Parameters)
            {
                if (p.GetType().IsSubclassOf(t))
                    if ( !editableOnly || ( editableOnly && p.Editable ) )
                        ret.Add(p);
            }

            return ret;
        }

        public static List<IParameter> GetParametersByType(Shader shader, Type t, bool editableOnly)
        {
            if (null == shader)
                return null;

            List<IParameter> ret = new List<IParameter>();
            
            foreach (IFusion fusion in shader.Fusions)
            {
                foreach (IParameter p in fusion.Parameters)
                {
                    if (p.GetType().IsSubclassOf(t))
                        if (!editableOnly || (editableOnly && p.Editable))
                            ret.Add(p);
                }
            }

            return ret;
        }

        public static List<IParameter> GetTextureParameters(Shader shader, bool editableOnly)
        {
            return GetParametersByType(shader, typeof(Parameter.TextureParamter), editableOnly);
        }

        public static List<IFusion> GetFusionsByClass(Shader shader, Stage stage)
        {
            if( null == shader)
                return null;

            List<IFusion> ret = new List<IFusion>();

            foreach (IFusion f in shader.Fusions)
            {
                if (f.Stage == stage)
                    ret.Add(f);
            }

            return ret;
        }

        public static List<IFusion> GetEnabledFusionsByClass(Shader shader, Stage cls)
        {
            if (null == shader)
                return null;

            List<IFusion> ret = new List<IFusion>();

            foreach (IFusion f in shader.Fusions)
            {
                if (f.Enabled && f.Stage == cls)
                    ret.Add(f);
            }

            return ret;
        }
    }
}
